package com.example.project.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.project.entity.Progress;
import com.example.project.entity.Project;
import com.example.project.entity.User;
import com.example.project.mapper.ProgressMapper;
import com.example.project.mapper.ProjectMapper;
import com.example.project.mapper.UserMapper;
import com.example.project.service.ProgressService;
import com.example.project.vo.model.PageDto;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;

/**
 * (Progress)表服务实现类
 */
@Service("progressService")
public class ProgressServiceImpl extends ServiceImpl<ProgressMapper, Progress>
        implements ProgressService {
    @Resource
    private ProgressMapper progressMapper;

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Progress queryById(Long id) {
        Progress progress = this.getBaseMapper().selectById(id);
        dealData(progress);
        return progress;
    }

    @Override
    public PageInfo<Progress> queryAll(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Progress> list = this.getBaseMapper().selectList(pageDto.toQueryWrapper());

        list.forEach(progress -> {
            dealData(progress);
        });

        return new PageInfo<>(list);
    }

    @Override
    public Progress insert(Progress progress) {
        this.getBaseMapper().insert(progress);
        return progress;
    }

    @Override
    public Progress update(Progress progress) {
        this.getBaseMapper().updateById(progress);
        return this.queryById(progress.getId());
    }

    @Override
    public List<Progress> querryList() {
        return this.getBaseMapper().selectList(null);
    }

    @Override
    public boolean deleteById(Long id) {
        return this.getBaseMapper().deleteById(id) > 0;
    }

    private void dealData(Progress progress){
        // 处理 user
        Long userId = progress.getCreateUser();
        if(ObjectUtils.isNotEmpty(userId)){
            User user = userMapper.selectById(userId);
            progress.setUser(user);
        }

        // 处理项目
        Long proId = progress.getProjId();
        if(ObjectUtils.isNotEmpty(proId)){
            Project project = projectMapper.selectById(proId);
            progress.setProject(project);
        }
    }
}
